package num88;

import java.util.Arrays;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/8 18:49
 * @Desc : https://leetcode-cn.com/problems/merge-sorted-array/submissions/
 */
public class Solution {
  public void merge(int[] nums1, int m, int[] nums2, int n) {
    int mInx = m - 1;
    int nInx = n - 1;
    int Inx = m + n - 1;
    while (nInx >= 0 && mInx >= 0) {
      if (nums1[mInx] >= nums2[nInx]) {
        nums1[Inx--] = nums1[mInx--];
      } else {
        nums1[Inx--] = nums2[nInx--];
      }
    }
    if (nInx >= 0) {
      for (int i = 0; i <= nInx; i++) {
        nums1[i] = nums2[i];
      }
    }
  }

  public static void main(String[] args) {
    int[] nums1 = {1, 2, 3, 0, 0, 0};
    int[] nums2 = {2,5,6};
    new Solution().merge(nums1, 3, nums2, 3);
    System.out.println(Arrays.toString(nums1));
  }
}
